package infostroy.bookagolf.components.dialogs;

import com.academysmart.bookagolf.i18n.Messages;
import com.academysmart.bookagolf.model.Equipment;
import com.academysmart.bookagolf.provider.EquipmentProvider;
import infostroy.bookagolf.common.Constants.Colors;
import infostroy.bookagolf.common.Constants.Dimensions;
import infostroy.bookagolf.components.controls.GolfRoundButton;
import infostroy.bookagolf.components.inputs.GolfDecimalTextField;
import infostroy.bookagolf.components.inputs.GolfTextField;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableModel;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * Created by Andrew on 25.02.14.
 */
public class GolfEquipmentEditDialog extends GolfBaseDialog {

    public static final String USD = "USD";
    public static final String PRICE = Messages.Order_price;
    public static final String UPDATE = Messages.GameUpdateEditingSupport_update;

    private JPanel mainPanel;
    private JPanel pricePanel;
    private JPanel nameEquipmentPanel;
    private JPanel buttonPanel;
    private JTextField equipmentName;
    private JTextField equipmentPrice;
    private JLabel priceLabel;
    private JLabel priceCurrencyLBL;
    private JButton editButton;

    private Equipment equipment;
    private int row;
    private TableModel model;

    public GolfEquipmentEditDialog(String title, Color background, Dimension dimension, Color titleColor) {
        super(title, background, dimension, titleColor);
        initPanels();
        initEquipmentPanelComponents();
        initButtonPanelComponents();
        initPricePanelComponents();
        addNameEquipmentPanelComponents();
        addPricePanelComponents();
        addButtonPanelComponents();
        addContentPanelComponents();
    }

    @Override
    protected void initContent() {
        if (equipment != null) {
            equipmentPrice.setText(String.valueOf(equipment.getPrice()));
            equipmentName.setText(equipment.getName());
        }

    }

    private void addContentPanelComponents() {
        mainPanel.add(nameEquipmentPanel);
        mainPanel.add(pricePanel);
        mainPanel.add(buttonPanel);
        this.contentPanel.add(mainPanel);
    }

    private void addButtonPanelComponents() {
        buttonPanel.add(Box.createRigidArea(new Dimension(80, 5)));
        buttonPanel.add(editButton, BorderLayout.CENTER);
    }

    private void addPricePanelComponents() {
        pricePanel.add(Box.createRigidArea(new Dimension(10, 5)));
        pricePanel.add(priceLabel);
        pricePanel.add(Box.createRigidArea(new Dimension(10, 5)));
        pricePanel.add(equipmentPrice);
        pricePanel.add(Box.createRigidArea(new Dimension(10, 5)));
        pricePanel.add(priceCurrencyLBL);
        pricePanel.add(Box.createRigidArea(new Dimension(10, 5)));
    }

    private void addNameEquipmentPanelComponents() {
        nameEquipmentPanel.add(Box.createRigidArea(new Dimension(10, 5)));
        nameEquipmentPanel.add(equipmentName);
        nameEquipmentPanel.add(Box.createRigidArea(new Dimension(10,5)));
    }

    private void initPricePanelComponents() {
        equipmentPrice = new GolfDecimalTextField();
        priceLabel = new JLabel(PRICE);
        priceCurrencyLBL = new JLabel(USD);
    }

    private void initButtonPanelComponents() {
        editButton = new GolfRoundButton(UPDATE, Colors.BLUE_BUTTON_BACKGROUND
                , Colors.BUTTON_FOREGROUND, Colors.BLUE_BUTTON_BORDER, Dimensions.DIALOG_EDIT_BUTTON_DIM);
        editButton.setBorder(new EmptyBorder(0,25,0,25));
        editButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                equipment.setName(equipmentName.getText());
                equipment.setPrice(Double.parseDouble(equipmentPrice.getText()));
                EquipmentProvider.INSTANCE.updateEquipment(equipment);
                model.setValueAt(equipmentName.getText(), row, 0);
                model.setValueAt(equipmentPrice.getText().concat(" USD"), row, 1);
                dialog.setVisible(false);
            }
        });
    }


    private void initPanels() {
        mainPanel = new JPanel(new GridLayout(3, 1));
        mainPanel.setBorder(new EmptyBorder(5,0,5,0));
        pricePanel = new JPanel();
        nameEquipmentPanel = new JPanel();
        buttonPanel = new JPanel();

        pricePanel.setLayout(new BoxLayout(pricePanel, BoxLayout.X_AXIS));
        nameEquipmentPanel.setLayout(new BoxLayout(nameEquipmentPanel, BoxLayout.X_AXIS));
        buttonPanel.setLayout(new BoxLayout(buttonPanel,BoxLayout.PAGE_AXIS));
    }

    private void initEquipmentPanelComponents() {
        equipmentName = new GolfTextField();
    }

    public void setDialogContent(int row, int id, TableModel model) {
        this.equipment = EquipmentProvider.INSTANCE.getEquipment(id);
        this.row = row;
        this.model = model;
    }
}
